<?php

namespace App\Console\Commands;

use App\support\gateway\Events;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Illuminate\Console\Command;
use Workerman\Worker;


class GatewayWorker extends Command
{
    /**
     * GatewayWorker控制器
     * @param $action string // start
     * @param $daemon any number of
     * @var string
     *  {--d : daemon mode}
     */
    protected $signature = 'gateway-worker:server {action} {--start=all : start}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '控制gataway-worker的命令程序';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $a = 1;
        global $argv;
        $action = $this->argument('action');

        // 针对windows不能同时启动三个服务的解决办法
        if ($action === 'single') {
            $start = $this->option('start');
            if ($start === 'register') {
                $this->startRegister();
            } elseif ($start === 'gateway') {
                $this->startGateWay();
            } elseif ($start === 'worker') {
                $this->startBusinessWorker();
            }
            Worker::runAll();

            return;
        }

        $argv[1] = $action;
//        $argv[2] = $this->option('daemon') ? '-d' : '';

        $this->start();
    }

    /*
     * gateway服务
     */
    private function start()
    {
        $this->startGateWay();     // gataway服务设置
        $this->startBusinessWorker(); // 设置businessWorker
        $this->startRegister();
        Worker::runAll();
    }

    private function startBusinessWorker()
    {
        $worker                  = new BusinessWorker();
        $worker->name            = 'BusinessWorker';   #设置BusinessWorker进程的名称
        $worker->count           = 2;                  #设置BusinessWorker进程的数量
        $worker->registerAddress = '127.0.0.1:12459';   #注册服务地址
        $worker->eventHandler    = Events::class; #设置使用哪个类来处理业务,业务类至少要实现onMessage静态方法，onConnect和onClose静态方法可以不用实现
    }


    private function startGateWay()
    {
        $gateway = new Gateway("websocket://127.0.0.1:23460");
        $gateway->name                 = 'chat';  #设置Gateway进程的名称，方便status命令中查看统计
        $gateway->count                = 1;          #进程的数量
        $gateway->lanIp                = '127.0.0.1'; #内网ip,多服务器分布式部署的时候需要填写真实的内网ip
        $gateway->startPort            = 2300;         #监听本机端口的起始端口
        $gateway->pingInterval         = 10;
        $gateway->pingNotResponseLimit = 1;             #是否需要客户端发送pingpong   1需要 0不需要
        $gateway->pingData             = '{"type":"ping"}';
        $gateway->registerAddress      = '127.0.0.1:12459';  #注册服务地址
    }

    private function startRegister()
    {
        new Register('text://127.0.0.1:12459');
    }
}
